#!/usr/bin/env bash
# ==============================================================================
# What:
# - master script for LOCAL-to-RDR client-side pipeline
# - invokes all sub-scripts in required order
# - environment variables are set in local2rdr.profile
# - individual sub-scripts can also be invoked manually
# ==============================================================================
export SELF=${0##*/}
export BASE_DIR="$( cd "$( dirname "$0" )" && pwd )"
source ${BASE_DIR}/local2rdr.profile


# Select desired Pipeline
# 1 = CSV-to-XML + XML transform + SCP
# 2 = XML transform + SCP
# 3 = SCP (same as directly running transferRIFCS.sh)
export TARGET_PIPELINE=${TARGET_PIPELINE:-1}


echo
echo "executing ${SELF} ..."

pushd $BASE_DIR > /dev/null

case "$TARGET_PIPELINE" in
   1) # Pipeline 1
      ./csv2xml.sh && ./xml2murifcs.sh && ./transferRIFCS.sh
      ;;
   2) # Pipeline 2
      ./xml2murifcs.sh && ./transferRIFCS.sh
      ;;
   3) # Pipeline 3
      ./transferRIFCS.sh
      ;;
   *) # Unsupported Pipeline number
      echo "  error: unsupported Pipeline number: ${TARGET_PIPELINE}"
      echo
      exit 1
      ;;
esac

if [[ $? = 0 ]]
then
  echo "finished: all tasks completed successfully"
  echo
  exit 0
else
  echo "failure: something went wrong"
  echo
  exit 1
fi
popd > /dev/null
